package cn.com.coding.system.service.impl;

import cn.com.coding.common.utils.PageUtil;
import cn.com.coding.common.utils.Query;
import cn.com.coding.system.dao.SysMenuDao;
import cn.com.coding.system.domin.SysMenuEntity;
import cn.com.coding.system.domin.SysUserEntity;
import cn.com.coding.system.domin.dto.SysMenuDto;
import cn.com.coding.system.service.SysMenuService;
import cn.com.coding.system.service.SysUserService;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        IPage<SysMenuEntity> page = this.page(new Query<SysMenuEntity>().getPage(params), new QueryWrapper<SysMenuEntity>());

        return new PageUtil(page);
    }

    @Override
    public List<SysMenuDto> getCurrentUserNav() {

        String userName = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        SysUserEntity sysUserEntity = sysUserService.getByUserName(userName);

        List<String> navMenuPids = sysUserService.getNavMenuPids(sysUserEntity.getPid());

        List<SysMenuEntity> sysMenuEntityList = (List<SysMenuEntity>) this.listByIds(navMenuPids);

        // 树状结构
        List<SysMenuEntity> menuEntityList = buildTreeMenu(sysMenuEntityList);

        // 实体转dto
        List<SysMenuDto> sysMenuDtoList = convert(menuEntityList);

        return sysMenuDtoList;
    }

    @Override
    public List<SysMenuEntity> tree() {
        // 获取所有菜单信息
        List<SysMenuEntity> sysMenus = this.list(new QueryWrapper<SysMenuEntity>().orderByAsc("order_num"));

        // 转成树状结构
        return buildTreeMenu(sysMenus);
    }

    private List<SysMenuDto> convert(List<SysMenuEntity> menuTree) {
        List<SysMenuDto> menuDtos = new ArrayList<>();

        menuTree.forEach(m -> {
            SysMenuDto dto = new SysMenuDto();

            dto.setPid(m.getPid());
            dto.setName(m.getPerms());
            dto.setTitle(m.getName());
            dto.setComponent(m.getComponent());
            dto.setPath(m.getPath());
            dto.setIcon(m.getIcon());

            if (m.getChildren().size() > 0) {

                // 子节点调用当前方法进行再次转换
                dto.setChildren(convert(m.getChildren()));
            }

            menuDtos.add(dto);
        });

        return menuDtos;
    }

    private List<SysMenuEntity> buildTreeMenu(List<SysMenuEntity> menus) {

        List<SysMenuEntity> finalMenus = new ArrayList<>();

        // 先各自寻找到各自的孩子
        for (SysMenuEntity menu : menus) {

            for (SysMenuEntity e : menus) {
                if (menu.getPid().equals(e.getParentPid())) {
                    menu.getChildren().add(e);
                }
            }

            // 提取出父节点
            if (menu.getParentPid().equals("0")) {
                finalMenus.add(menu);
            }
        }

        System.out.println(JSONUtil.toJsonStr(finalMenus));
        return finalMenus;
    }


}